﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;


public static class MiaoPath
{
    /// <summary>
    /// 返回文件所在目录，既XXX_Data位置。
    /// </summary>
    public static DirectoryInfo GetDataDirectoryInfo
    {
        get
        {
            DirectoryInfo directoryInfo;
#if UNITY_EDITOR
            //文件打包位于XXX_Data的位置，位于exe运行文件的下一级。
            //如果没打包则位于Assets位置,不带斜杠。
            directoryInfo = new DirectoryInfo(Application.dataPath).Parent;
#else
                directoryInfo = new DirectoryInfo(Application.dataPath);
#endif
            return directoryInfo;
        }
    }

    public static string GetFullPath
    {
        get
        {
            DirectoryInfo directoryInfo;
#if UNITY_EDITOR
            //文件打包位于XXX_Data的位置，位于exe运行文件的下一级。
            //如果没打包则位于Assets位置,不带斜杠。
            directoryInfo = new DirectoryInfo(Application.dataPath).Parent;
           
#else
    directoryInfo = new DirectoryInfo(Application.dataPath);

#endif
            return directoryInfo.FullName;

        }
    }


    /// <summary>
    /// 获取目录下的所有文件
    /// </summary>
    /// <param name="folderPath"></param>
    /// <param name="endWith"></param>
    /// <returns></returns>
    public static string[] GetFolderFiles(string folderPath, string endWith = "")
    {
        List<string> result = new List<string>();
        if (Directory.Exists(folderPath))
        {
            return Directory.GetFiles(folderPath);
        }
        return null;
    }

    /// <summary>
    /// 获取对应路径的相对路径（如absolutePath=E:/AB/c.txt,relativeTo=E:/AB/，输出c.txt）
    /// </summary>
    /// <param name="absolutePath"></param>
    /// <param name="relativeTo"></param>
    /// <returns></returns>
    public static string GetRelativePath(string absolutePath, string relativeTo)
    {
        var fileInfo = new FileInfo(relativeTo);
        var fullFileInfo = new FileInfo(absolutePath);
        string absoluteName = fullFileInfo.FullName;
        absoluteName = MakePathPerfect(absoluteName);
        string relative = fileInfo.FullName;
        relative = MakePathPerfect(relative);
        string result = absoluteName.Replace(relative, "");
        return result;
    }


    /// <summary>
    /// 使路径规范化(都变成这样的格式：Assets/Game/Source)
    /// 如Assets\Game\Source变成Assets/Game/Source
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string MakePathPerfect(string path)
    {
        return path.Replace("\\", "/");
    }

}

